package org.lcc.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.lcc.Service.TestResultsService;
import org.lcc.model.VO.ExamAndAnswerVO;
import org.lcc.model.VO.ExamScoreDetailVO;
import org.lcc.model.VO.ProcessedMistakeVO;
import org.lcc.model.VO.StudentgradeinquiryVo;
import org.lcc.model.dto.GetExamScoreListDto;
import org.lcc.model.entity.BusinessException;
import org.lcc.util.ApiResponseMe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping()
@Tag(name = "考试成绩", description = "成绩相关的接口")
public class TestResultsController {
    @Autowired
    private TestResultsService testResultsService;

    @PostMapping("/getExamScoreList")
    public ApiResponseMe getExamScoreList(@RequestBody GetExamScoreListDto getExamScoreListDto) {
        System.out.println("getExamScoreList"+getExamScoreListDto);
        if (getExamScoreListDto.getPageNum() == null || getExamScoreListDto.getPageSize() == null) {
            return ApiResponseMe.error(400, "页码和每页记录数不能为空");
        }
        Map<String, Object> result = testResultsService.getExamScoreList(getExamScoreListDto);
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("data", result.get("data"));
        responseMap.put("total", result.get("total"));
        return ApiResponseMe.success(responseMap);
    }

    @Operation(summary = "获取考试详情分析", description = "根据考试ID获取考试详情分析数据")
    @GetMapping("/getExamScoreDetail")
    public ApiResponseMe getExamScoreDetail(@Parameter(description = "考试ID") @RequestParam Integer examId) {
        if (examId == null) {
            return ApiResponseMe.error(400, "考试ID不能为空");
        }

        ExamScoreDetailVO detailVO = testResultsService.getExamScoreDetail(examId);
        if (detailVO == null) {
            return ApiResponseMe.error(400, "考试不存在");
        }

        return ApiResponseMe.success(detailVO);
    }
    @Operation(summary = "获取班级信息", description = "根据老师工号获取班级信息数据")
    @GetMapping("/getClassID")
    public ApiResponseMe getExamClass(@Parameter(description = "老师工号(schoolNumber)") @RequestParam String teacherId) {
        if (teacherId == null) {
            return ApiResponseMe.error(400, "老师工号不能为空");
        }

        List< Map<String, Long> > classIdMap = testResultsService.getExamClass(teacherId);
        if (classIdMap == null) {
            return ApiResponseMe.error(400, "考试不存在");
        }

        return ApiResponseMe.success(classIdMap);
    }
    @Operation(summary = "获取试卷信息", description = "根据班级ID试卷信息数据")
    @GetMapping("/getExam")
    public ApiResponseMe getExam(@Parameter(description = "班级ID(ClassID)") @RequestParam String classID) {
        if (classID == null) {
            return ApiResponseMe.error(400, "请选择班级");
        }

        List< Map<String, Long> > classIdMap = testResultsService.getExam(classID);
        if (classIdMap == null) {
            return ApiResponseMe.error(400, "考试不存在");
        }

        return ApiResponseMe.success(classIdMap);
    }
    @Operation(summary = "获取饼形图信息", description = "根据班级ID/考试ID获取饼形图数据")
    @GetMapping("/getPieChart")
    public ApiResponseMe getPieChart(@Parameter(description = "班级ID(ClassID)") @RequestParam String classID,@Parameter(description = "考试ID(ExamID)") @RequestParam String examId) {
        if (classID == null) {
            return ApiResponseMe.error(400, "请选择班级");
        }
        if (examId == null) {
            return ApiResponseMe.error(400, "请选择考试");
        }

        String analysis_data = testResultsService.getPieChart(classID,examId);
        if (analysis_data == null) {
            return ApiResponseMe.error(400, "该考试不存在数据");
        }

        return ApiResponseMe.success(analysis_data);
    }
    //学生端成绩查询
    @Operation(summary = "获取学生成绩", description = "根据学生ID获取学生成绩数据")
    @GetMapping("/Studentgradeinquiry")
    public ApiResponseMe Studentgradeinquiry(@Parameter(description = "学生ID(studentID)") @RequestParam("schoolNumber") String studentID) {
        if (studentID == null){
            return ApiResponseMe.error(400, "学生ID不能为空");
        }
        List<StudentgradeinquiryVo> studentgradeinquiryVo =testResultsService.Studentgradeinquiry(studentID);
        if (studentgradeinquiryVo == null) {
            return ApiResponseMe.error(400, "该学生暂无考试");
        }
        return ApiResponseMe.success(studentgradeinquiryVo);
    }
    //学生端查看详情
    @Operation(summary = "获取学生成绩详情", description = "根据考试ID和学生学号获取学生成绩详情数据")
    @GetMapping("/Studentresultsfordetails")
    public ApiResponseMe Studentresultsfordetails(@Parameter(description = "考试ID(examID)") @RequestParam("examId") String examId,@Parameter(description = "学生ID(studentID)") @RequestParam("studentId") String studentID) {
        if (examId == null){
            return ApiResponseMe.error(400, "考试ID不能为空");
        }
        if (studentID == null){
            return ApiResponseMe.error(400, "学生ID不能为空");
        }
        ExamAndAnswerVO examAndAnswerVO =testResultsService.Studentresultsfordetails(examId,studentID);
        if (examAndAnswerVO == null) {
            return ApiResponseMe.error(400, "该学生暂无考试");
        }
        return ApiResponseMe.success(examAndAnswerVO);
    }
   //修改邮箱
    @Operation(summary = "修改邮箱", description = "修改邮箱")
    @GetMapping("/updateEmail")
    public ApiResponseMe updateEmail( @RequestParam String password, @RequestParam String newEmail,@RequestParam String username) {
        System.out.println(password);
        System.out.println(newEmail);
        System.out.println(username);
        if (password == null) {
            return ApiResponseMe.error(400, "密码不能为空");
        }
        if (newEmail == null) {
            return ApiResponseMe.error(400, "新邮箱不能为空");
        }
        if (username == null) {
            return ApiResponseMe.error(400, "用户名不能为空");
        }
        try {
            testResultsService.updateEmail(username,password,newEmail);
            return ApiResponseMe.success("修改成功");
        }catch (BusinessException e){
            return ApiResponseMe.error(e.getCode(), e.getMessage());
        }
    }
    //修改密码
    @Operation(summary = "修改密码", description = "修改密码")
    @GetMapping("/updatePassword")
    public ApiResponseMe updatePassword( @RequestParam String oldPassword, @RequestParam String newPassword,@RequestParam String username) {
        System.out.println("加密传输的旧密码"+oldPassword);
        System.out.println("加密传递的新密码" +newPassword);
        System.out.println(username);
        if (oldPassword == null) {
            return ApiResponseMe.error(400, "旧密码不能为空");
        }
        if (newPassword == null) {
            return ApiResponseMe.error(400, "新密码不能为空");
        }
        try {
            testResultsService.updatePassword(username,oldPassword,newPassword);
            return ApiResponseMe.success("修改成功");
        }catch (BusinessException e){
            return ApiResponseMe.error(e.getCode(), e.getMessage());
        }
    }
    @Operation(summary = "获取错题本", description = "获取学生的错题本记录")
    @GetMapping("/getMistakeCollection")
    public ApiResponseMe getMistakeCollection(
        @Parameter(description = "学生ID") @RequestParam String studentID,
        @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
        @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
        @Parameter(description = "搜索内容") @RequestParam(required = false) String searchContent,
        @Parameter(description = "状态筛选") @RequestParam(required = false) String status
    ) {
        if (studentID == null) {
            return ApiResponseMe.error(400, "学生ID不能为空");
        }
        
        Map<String, Object> result = testResultsService.getMistakeCollection(
            studentID, pageNum, pageSize, searchContent, status
        );
        
        return ApiResponseMe.success(result);
    }
    //错题状态更新
    @Operation(summary = "更新错题状态", description = "更新错题状态")
    @GetMapping("/updateMistakeStatus/{mistake_id}/{status}")
    public ApiResponseMe updateMistakeStatus(@Parameter(description = "错题ID")@PathVariable Integer mistake_id,@Parameter(description = "状态")@PathVariable String status){
        if (mistake_id == null|| status == null){
            return ApiResponseMe.error(400, "参数不能为空");
        }
        try {
            testResultsService.updateMistakeStatus(mistake_id,status);
            return ApiResponseMe.success("更新成功");
        }catch (BusinessException e){
            return ApiResponseMe.error(e.getCode(), e.getMessage());
        }
    }
    @Operation(summary = "获取学生所属班级信息", description = "根据学生ID获取所属班级和教师信息")
    @GetMapping("/getStudentClassInfo")
    public ApiResponseMe getStudentClassInfo(@Parameter(description = "学生ID") @RequestParam String studentID) {
        if (studentID == null) {
            return ApiResponseMe.error(400, "学生ID不能为空");
        }
        
        List<Map<String, Object>> classInfo = testResultsService.getStudentClassInfo(studentID);
        if (classInfo == null) {
            return ApiResponseMe.error(400, "未找到该学生的班级信息");
        }
        
        return ApiResponseMe.success(classInfo);
    }
}
